package cn.edu.nju.notification;

import cn.edu.nju.R;
import cn.edu.nju.newsFeed.ListItemFormatHelper;
import cn.edu.nju.newsFeed.NewsFeedItem;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class AtNotificationActivity extends Activity{
	private ListItemFormatHelper helper;
	private AtNotification notification;
	private ListView commentsList;
	private AtCommentListAdapter listAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_feed_item_detail);
		
		this.helper = new ListItemFormatHelper(this);
		
		NotificationProvider provider = NotificationProvider.getInstance(this);
		Intent intent = getIntent();
		int position = (Integer) intent.getIntExtra(NotificationActivity.AT_NOTIFICATION_POSITION, 0);
		this.notification = provider.getAtNotifications().get(position);
		
		// init widget
		initTitleText();
		initRefreshButton();
		initReplyButton();
		initSharePost();
		initCommentsList();
	}
	
	private void initTitleText(){
		TextView titleText = (TextView) findViewById(R.id.title_text);
		String title = getString(R.string.at_notification);
		titleText.setText(title);
	}
	
	private void initRefreshButton(){
		Button refreshButton = (Button) findViewById(R.id.refresh_button);
		refreshButton.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {
				notification.refresh();
				listAdapter.refresh();
				updateSharePost();
				commentsList.setSelectionFromTop(0, 0);
			}
		});
	}
	
	private void initReplyButton(){
		
	}
	
	private void initSharePost(){
		NewsFeedItem feedItem = notification.getNewsFeedItem();
		
		View postView = findViewById(R.id.share_post_item);
		
		postView.setBackgroundResource(R.color.lighter_blue);
		
		ImageView avatarView = (ImageView) postView.findViewById(R.id.avatar);
		helper.setAvatar(avatarView, feedItem);
		
		TextView contentView = (TextView) postView.findViewById(R.id.feed_item_text);
		helper.setContentText(contentView, feedItem, null, null);
		
		TextView timeView = (TextView)postView.findViewById(R.id.feed_time_text);
		helper.setTimeText(timeView, feedItem);
		
		ImageView previewImageView = (ImageView) postView.findViewById(R.id.feed_item_image_preview);
		helper.setPreviewImage(previewImageView, feedItem, false);
		
		TextView commentsNumberView = (TextView)postView.findViewById(R.id.comments_number_text);
		helper.setCommentNumberText(commentsNumberView, feedItem, null);
		
		TextView likeNumberView = (TextView)postView.findViewById(R.id.like_number_text);
		helper.setLikeNumberText(likeNumberView, feedItem, null);
	}
	
	private void initCommentsList(){
		commentsList = (ListView)findViewById(R.id.comments_list);
		listAdapter = new AtCommentListAdapter(this, notification);
		commentsList.setAdapter(listAdapter);
	}
	
	private void updateSharePost(){
		NewsFeedItem feedItem = notification.getNewsFeedItem();
		
		View postView = findViewById(R.id.share_post_item);
		
		TextView timeView = (TextView)postView.findViewById(R.id.feed_time_text);
		helper.setTimeText(timeView, feedItem);
		
		TextView commentsNumberView = (TextView)postView.findViewById(R.id.comments_number_text);
		helper.setCommentNumberText(commentsNumberView, feedItem, null);
		
		TextView likeNumberView = (TextView)postView.findViewById(R.id.like_number_text);
		helper.setLikeNumberText(likeNumberView, feedItem, null);
	}
}
